package com.base.cn.platform.os.controller.mobile.marketing;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.utils.action.ActionKey;
import com.base.cn.platform.os.service.manage.marketing.baseShare.MarketingBaseShareService;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * H5营销Controller
 *
 * @create 2019-04-10-15:14
 */
@Controller
@RequestMapping("/mobile/uc/marketing")
public class MobileMarketingController extends MobileBaseController {

    @Autowired
    private MarketingBaseShareService marketingBaseShareService;

    @Autowired
    private CusUserIntegralService cusUserIntegralService;

    @Value("${"+CommonConstants.STATIC_PATH+"}")
    private String staticPath;
    /**
    * 基础分享海报
    */
    @RequestMapping("/baseShare")
    public String baseShare(HttpServletRequest request,
                       @RequestParam(value = "shareUrl",required = false) String shareUrl,
                       @RequestParam(value = "title",required = false) String title,
                       @RequestParam(value = "lookNum",required = false,defaultValue = "0") int lookNum) throws UnsupportedEncodingException {
        request.setAttribute("shareUrl",shareUrl);
        request.setAttribute("title",title);
        request.setAttribute("lookNum",lookNum);
        JsonObject user = webMobileLoginUtil.getLoginUser(request);
        JsonElement avatarObj = user.get("avatar");

        String avatar = "";
        if(avatarObj!=null && !avatarObj.isJsonNull()){
            avatar = avatarObj.getAsString();
            if(!avatar.startsWith(staticPath)){
                try{
                    URL urlConet = new URL(avatar);
                    HttpURLConnection con = (HttpURLConnection)urlConet.openConnection();
                    con.setRequestMethod("GET");
                    con.setConnectTimeout(4 * 1000);
                    InputStream inStream = con .getInputStream();    //通过输入流获取图片数据
                    byte[] buffer = new byte[inStream.available()];
                    inStream.read(buffer);
                    BASE64Encoder encoder = new BASE64Encoder();
                    avatar = "data:image/png;base64,"+encoder.encode(buffer);// 返回Base64编码过的字节数组字符串
                }catch (Exception e){
                    avatar = "";
                    logger.error("baseShare()--error",e);
                }
            }
        }
        request.setAttribute("avatar",avatar);
        JsonElement nickNameObj = user.get("nickname");
        String nickname = "";
        if(nickNameObj !=null && !nickNameObj.isJsonNull()){
            nickname = nickNameObj.getAsString();
        }
        request.setAttribute("username",nickname);

        Map<String,Object> params = new HashMap<>();
        Map<String,Object> baseShare = marketingBaseShareService.queryBaseShare(params);
        request.setAttribute("baseShare",baseShare);
        return resultPage("/marketing/baseShare/base-share-inx");
    }

}
